package com.smart.web.controller.user.hik;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

@RestController
public class FileDownloads {
    @PostMapping("/fileUp")
    public boolean fileUp(@RequestBody String json){
        //判断是否为JSON
        if(!json.startsWith("{")){
            downloadFile(json);
            return true;
        }
//        https://xhqs.oss-cn-hangzhou.aliyuncs.com/moment/moment/202307051600452023070516004507520577724.jpg
        JSONObject jsonObject = JSONObject.parseObject(json);
        Object param = jsonObject.get("param");
        JSONArray jsonArray = (JSONArray) jsonObject.get("data");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject js = jsonArray.getJSONObject(i);
            Object headImage = js.get(param.toString());
            String s = headImage.toString();
            //获取是否存在逗号,如果存在的话,那么就表示为多个图片
            if(s.contains(",")){
                for (String s1 : s.split(",")) {
                    downloadFile(s1);
                }
            }else{
                downloadFile(s);
            }

        }
        return true;
    }

    public static void main(String[] args) {
        String s = "https://xhqs.oss-cn-hangzhou.aliyuncs.com/phone/202306171209202023061712092000170459698.jpeg";
        int i1 = s.lastIndexOf("/");
        String substring = s.substring(i1 + 1);
        System.out.println(substring);
//        downloadFile("https://xhqs.oss-cn-hangzhou.aliyuncs.com/phone/202306171209202023061712092000170459698.jpeg", "C:\\Users\\Administrator\\Desktop\\phone\\202306171209202023061712092000170459698.jpeg");
    }
    public static void downloadFile(String remoteFilePath) {
        String parentFolder = "C:\\Users\\Administrator\\Desktop\\images";
        URL website = null;
        ReadableByteChannel rbc = null;
        FileOutputStream fos = null;
        try {
            int i1 = remoteFilePath.lastIndexOf("/");
            String fileName = remoteFilePath.substring(i1 + 1);
            //去掉头部部分
            String folder = remoteFilePath.replaceAll("https://xhqs.oss-cn-hangzhou.aliyuncs.com", "");
            //去掉名称部分
            folder = folder.replaceAll(fileName, "");
            //获取所有文件夹
            website = new URL(remoteFilePath);
            rbc = Channels.newChannel(website.openStream());
            new File(parentFolder+folder).mkdirs();
            String url = parentFolder+folder+fileName;
            System.out.println(url);
            fos = new FileOutputStream(url);//本地要存储的文件地址 例如：test.txt
            fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(rbc!=null){
                try {
                    rbc.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}
